var base64 = require("../weui/example/images/base64");
//获取应用实例
const app = getApp()

Page({
  data: {
    remark: '',
    id: '',
    record: null,
    subjectDisplay: '1',
    resultRemark: '',
    n: 0,
    wh:0,
    resultOption: [],
    items: [
      { value: 'A', name: 'A' },
      { value: 'B', name: 'B' },
      { value: 'C', name: 'C' },
      { value: 'D', name: 'D' }
    ]
  },
  onShow: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          "wh": res.windowHeight - 96 -wx.getSystemInfoSync().statusBarHeight
        })
      }
    })
  },
  goBack: function(){
    wx.navigateBack({
      delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
    });
  },
  onLoad: function (options) {
    wx.setNavigationBarColor({
      backgroundColor: '#C40002',
      frontColor: '#ffffff'
    })
    var id = options.id;
    this.setData({
      id: id
    })
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/exam/detail',
      data: {
        id: id,
        userId: app.globalData.userId
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        if (res.data.syscode == 0) {
          var sd = '0';
          if (res.data.record.examResult == '考试通过'){
            sd = '2'
          }
          else if (res.data.record.examResult == '考试未通过'){
            sd = '3'
          }
          that.data.record = res.data.record;
          that.setData({
            subjectDisplay: sd,
            record: that.data.record,
            remark: that.data.record.remark,
            resultRemark: res.data.record.resultRemark
          })
        }
      }
    })
  },
  startExam: function() {
    this.setData({
      subjectDisplay: '1'
    })
  },
  next: function () {
    //判断是否选择
    if (this.data.resultOption.length < this.data.n + 1 || this.data.resultOption[this.data.n].length == 0) {
      wx.showToast({
        title: '请先选择答案',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.n < this.data.record.subjectList.length - 1) {
      //切换页码
      var num = this.data.n + 1;
      this.setData({
        n: num
      })
      //初始化checkbox
      var items = this.data.items;
      for (let i = 0, lenI = items.length; i < lenI; ++i) {
        items[i].checked = false
      }
      if (this.data.resultOption.length > this.data.n) {
        for (let i = 0, lenI = items.length; i < lenI; ++i) {
          for (let j = 0, lenJ = this.data.resultOption[this.data.n].length; j < lenJ; ++j) {
            if (items[i].value === this.data.resultOption[this.data.n][j]) {
              items[i].checked = true
              break
            }
          }
        }
      }
      this.setData({
        items: items
      })
    }
    else {
      var subjectSize = this.data.record.subjectList.length;
      var resultOp = this.data.resultOption;
      var total = subjectSize;
      var success = 0;
      for (let a = 0; a < subjectSize; a++) {
        var rightTimes = 0;
        var answerSize = this.data.record.subjectList[a].answerList.length;
        //获取正确答案
        var ansStr = "";
        for (let b = 0; b < answerSize; b++) {
          var answerItem = this.data.record.subjectList[a].answerList[b];
          if (answerItem.isRight == 1){
            ansStr += answerItem.optionSort
          }
        }
        //判断选择项是否包含在答案中
        var resItem = resultOp[a]
        if (ansStr.length == resItem.length) {
          for (let k = 0; k < resItem.length; k++) {
            if (ansStr.indexOf(resItem[k]) >= 0) {
              rightTimes++;
            }
          }
          if (rightTimes == resItem.length){
            success++;
          }
        }
      }
      var sd = '3';
      var examResult = "";
      var resultRemark = "结果详情：共" + total + "题，共答对" + success + "题";
      var examTime = 0;
      if (total == success) {
        sd = '2';
        examResult = "考试通过";
      }
      else {
        sd = '3';
        examResult = "考试未通过";
      }
      var that = this;
      wx.request({
        url: 'https://www.xinhuo.site/api/exam/updateExamResult',
        data: {
          examId: this.data.id,
          userId: app.globalData.userId,
          examResult: examResult,
          resultRemark: resultRemark,
          examTime: examTime
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success(res) {
          if (res.data.syscode == 0) {
            that.setData({
              resultRemark: resultRemark,
              subjectDisplay: sd
            })
          }
        }
      })
    }
  },
  pre: function () {
    if (this.data.n > 0) {
      var num = this.data.n - 1;
      this.setData({
        n: num
      })
      //初始化checkbox
      var items = this.data.items;
      for (let i = 0, lenI = items.length; i < lenI; ++i) {
        items[i].checked = false
      }
      for (let i = 0, lenI = items.length; i < lenI; ++i) {
        for (let j = 0, lenJ = this.data.resultOption[this.data.n].length; j < lenJ; ++j) {
          if (items[i].value === this.data.resultOption[this.data.n][j]) {
            items[i].checked = true
            break
          }
        }
      }
      this.setData({
        items: items
      })
    }
    else {
      this.setData({
        subjectDisplay: '0'
      })
    }
  },
  back: function () {
    wx.navigateBack({
      delta: 1
    });
  },
  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)

    const items = this.data.items
    const values = e.detail.value
    var num = this.data.n;
    this.data.resultOption[num] = values;

    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false

      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value === values[j]) {
          items[i].checked = true
          break
        }
      }
    }
    console.log('resultOption：', this.data.resultOption)
    this.setData({
      items: items
    })
  }
})
